/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Scene;

import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import mygame.Backbone.GlobalVars;
import mygame.Manager;

/**
 *
 * @author Mr.N0I3oDy
 */
public class PlacementTriangle{

    private boolean isUsed = false;
    private Vector3f pos;
    private Vector3f normal;

    public PlacementTriangle() {
        pos = new Vector3f();
        normal = new Vector3f();
        //initCustomFields();
    }

    public PlacementTriangle(Vector3f pos,Vector3f normal) {
        this.pos =  pos;
        this.normal = normal;
        //initCustomFields();
    }

    /**
     * Places and holds a GameObject on the center of the triangle.
     * @param object the GameObject to place
     */
    public boolean placeObject(Spatial object) {
        if (!isUsed) {
            object.setLocalTranslation(this.pos.mult(Manager.GetInstance().getSceneManager().getGround().getLocalScale()));
            object.rotateUpTo(this.normal);

            //rotate randomly
            object.rotate(0, GlobalVars.random.nextInt(12), 0);
            Manager.GetInstance().getModelManager().attach(object);
            isUsed = true;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Removes the GameObject from the Triangle and returns the GameObject to the calling function
     * @return the GameObject of the Triangle
     */
    public void removeObject() {
        if (isUsed) {
            isUsed = false;
        }
    }

    public boolean hasObject() {
        if (isUsed) {
            return true;
        } else {
            return false;
        }
    }
//    public String save() {
//        String s = GlobalVars.DELIMITER_ATTRIBUTE;
//        s += get1().x + "," + get1().y + "," + get1().z + "," + get2().x + "," + get2().y + "," + get2().z + "," + get3().x + "," + get3().y + "," + get3().z;
//        return s;
//    }
//
//    public static PlacementTriangle load(String l) {
//        String[] o = l.split(GlobalVars.DELIMITER_ATTRIBUTE);
//        String[] attr = o[1].split(",");
//        Vector3f p1 = new Vector3f(Float.parseFloat(attr[0]), Float.parseFloat(attr[1]), Float.parseFloat(attr[2]));
//        Vector3f p2 = new Vector3f(Float.parseFloat(attr[3]), Float.parseFloat(attr[4]), Float.parseFloat(attr[5]));
//        Vector3f p3 = new Vector3f(Float.parseFloat(attr[6]), Float.parseFloat(attr[7]), Float.parseFloat(attr[8]));
//        return new PlacementTriangle(p1, p2, p3);
//    }
    
    public Vector3f getPosition(){
        return this.pos;
    }
}
